package com.ruoyi.web.controller.device;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Device;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.DeviceService;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/9/13
 **/
@RestController
@RequestMapping("/device/device")
public class DeviceController extends BaseController {
    @Resource
    private DeviceService deviceService;

    @Log(title = "查询设备列表", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectList(device);
        return getDataTable(list);
    }

    @Log(title = "查询设备列表", businessType = BusinessType.OTHER)
    @GetMapping("/countList")
    public AjaxResult countList(Device device) {
        List<Device> list = new ArrayList<>();

        List<String> type = Arrays.asList("水温监测设备", "水压监测设备", "智能照明设备");
        for (String s : type) {
//            List<Device> exList = new ArrayList<>();
            Device q = new Device();
            q.setType(s);
            q.setStatus("运行中");
//            if (s.equals("智能照明设备")) {
//                q1.setStatus("启用");
//                q.setStatus("启用");
//
//            } else {
//                q1.setStatus("运行中");
//                q.setStatus("启用");
//                exList = deviceService.selectExList(q);
//            }
//            q1.setType(s);

            List<Device> exList = deviceService.selectExList(q);
            List<Device> normalList = deviceService.selectList(q);
            if (!CollectionUtils.isEmpty(normalList)) {
                q.setCount1(normalList.size());
            } else {
                q.setCount1(0);
            }

            if (!CollectionUtils.isEmpty(exList)) {
                q.setCount2(exList.size());
            } else {
                q.setCount2(0);
            }
            list.add(q);
        }

        return AjaxResult.success(list);
    }

    @Log(title = "设备列表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备数据");
    }

    @Log(title = "查询人员管理All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(Device device) {
        List<Device> list = deviceService.selectList(device);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(deviceService.selectById(id));
    }

    @Log(title = "新增人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Device device) {
        device.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = deviceService.create(device);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Device device) {
        device.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = deviceService.update(device);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(deviceService.deleteById(id));
    }

}
